﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using T.FAS.Runtime.Action.SPI;
using T.ServiceComponent.Cache.Service;

namespace T.FAS.Runtime.Action.Service
{
    class ActionRTPRedisCache
    {
        private static CacheService cacheService = null;
        private string FASSchemaKey = "FASSchema";
        private string TimeStampKey = "TimeStamp";

        public ActionRTPRedisCache()
        {
            cacheService = CacheService.GetInstance("DefaultPool");
        }

        public long GetTimeStamp(string metaDataId)
        {
            var hashId = this.GetHashId(metaDataId);
            using (ICacheClient client = cacheService.GetClient())
            {
                return client.GetValueFromHash<long>(hashId, TimeStampKey);
            }
        }
        public void Add(ActionInfoWithSchema schema)
        {
            if (schema == null)
                return;

            var hashId = this.GetHashId(schema.ActionID);
            var hashCode = this.GetHashId(schema.ActionCode);
            using (ICacheClient client = cacheService.GetClient())
            {
                client.SetEntryInHash<ActionInfoWithSchema>(hashId, FASSchemaKey,schema);
                client.SetEntryInHash<string>(hashCode, FASSchemaKey,schema.ActionID);
                var timestamp = ActionRTPUtil.GetTimeStamp(schema.LastModifyTime);
                client.SetEntryInHash<long>(hashId, TimeStampKey, timestamp);
                client.SetEntryInHash<long>(hashCode,TimeStampKey,timestamp);
            }
        }

        public ActionInfoWithSchema Get(string metaDataId)
        {
            var hashIdOrCode = this.GetHashId(metaDataId);
            string hashId = hashIdOrCode;
            var matchResult = Regex.Match(metaDataId, @"[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", RegexOptions.IgnoreCase);
            using (ICacheClient client = cacheService.GetClient())
            {
                if (matchResult.Success == false)
                {
                    var metaId = client.GetValueFromHash<string>(hashIdOrCode, FASSchemaKey);
                    hashId = this.GetHashId(metaId);
                }
                if (string.IsNullOrEmpty(hashId) == false)
                {
                    var schema = client.GetValueFromHash<ActionInfoWithSchema>(hashId, FASSchemaKey);
                    return schema;
                }
                return null;
            }
        }

        public void Remove(string metaDataId)
        {
            var hashIdOrCode = this.GetHashId(metaDataId);
            string hashId = hashIdOrCode;
            var matchResult = Regex.Match(metaDataId, @"[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", RegexOptions.IgnoreCase);
            using (ICacheClient client = cacheService.GetClient())
            {
                if (matchResult.Success == false)
                {
                    var metaId = client.GetValueFromHash<string>(hashIdOrCode, FASSchemaKey);
                    hashId = this.GetHashId(metaId);
                }
                client.RemoveEntry(hashId);
            }
        }
        private string GetHashId(string metaDataId)
        {
            return "FASSchema:" + metaDataId;
        }
    }
}
